package org.dromara.vehicle.modules.driver.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.vehicle.modules.driver.domain.DriverBasicInfo;
import org.dromara.vehicle.modules.driver.domain.DriverTrainingRecord;
import org.dromara.vehicle.modules.driver.domain.DriverViolationRecord;
import org.dromara.vehicle.modules.driver.domain.bo.DriverBasicInfoBo;
import org.dromara.vehicle.modules.driver.domain.vo.DriverBasicInfoVo;
import org.dromara.vehicle.modules.driver.domain.vo.DriverTrainingRecordVo;
import org.dromara.vehicle.modules.driver.domain.vo.DriverViolationRecordVo;
import org.dromara.vehicle.modules.driver.mapper.DriverBasicInfoMapper;
import org.dromara.vehicle.modules.driver.service.IDriverBasicInfoService;
import org.dromara.vehicle.modules.driver.service.IDriverTrainingRecordService;
import org.dromara.vehicle.modules.driver.service.IDriverViolationRecordService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 驾驶员基本信息Service业务层处理
 *
 * @author ckk
 * @date 2025-06-24
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class DriverBasicInfoServiceImpl extends ServiceImpl<DriverBasicInfoMapper, DriverBasicInfo> implements IDriverBasicInfoService {

    private final DriverBasicInfoMapper baseMapper;
    private final IDriverViolationRecordService driverViolationRecordService;
    private final IDriverTrainingRecordService driverTrainingRecordService;

    /**
     * 查询驾驶员基本信息
     *
     * @param id 主键
     * @return 驾驶员基本信息
     */
    @Override
    public DriverBasicInfoVo queryById(Long id) {
        DriverBasicInfoVo vo = baseMapper.selectVoById(id);
        Assert.notNull(vo, () -> new ServiceException("驾驶员基本信息不存在"));

        // 获取驾驶员违章记录
        vo.setViolationRecords(BeanUtil.copyToList(driverViolationRecordService.lambdaQuery().eq(DriverViolationRecord::getDriverId, id).list(), DriverViolationRecordVo.class));

        // 获取驾驶员培训记录
        vo.setTrainingRecords(BeanUtil.copyToList(driverTrainingRecordService.lambdaQuery().eq(DriverTrainingRecord::getDriverId, id).list(), DriverTrainingRecordVo.class));

        return vo;
    }

    /**
     * 分页查询驾驶员基本信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 驾驶员基本信息分页列表
     */
    @Override
    public TableDataInfo<DriverBasicInfoVo> queryPageList(DriverBasicInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DriverBasicInfo> lqw = buildQueryWrapper(bo);
        Page<DriverBasicInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的驾驶员基本信息列表
     *
     * @param bo 查询条件
     * @return 驾驶员基本信息列表
     */
    @Override
    public List<DriverBasicInfoVo> queryList(DriverBasicInfoBo bo) {
        LambdaQueryWrapper<DriverBasicInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DriverBasicInfo> buildQueryWrapper(DriverBasicInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DriverBasicInfo> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), DriverBasicInfo::getName, bo.getName());
        lqw.like(StringUtils.isNotBlank(bo.getIdNumber()), DriverBasicInfo::getIdNumber, bo.getIdNumber());
        lqw.like(StringUtils.isNotBlank(bo.getLicenseNumber()), DriverBasicInfo::getLicenseNumber, bo.getLicenseNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getLicenseType()), DriverBasicInfo::getLicenseType, bo.getLicenseType());
        return lqw;
    }

    /**
     * 新增驾驶员基本信息
     *
     * @param bo 驾驶员基本信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(DriverBasicInfoBo bo) {
        DriverBasicInfo add = MapstructUtils.convert(bo, DriverBasicInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改驾驶员基本信息
     *
     * @param bo 驾驶员基本信息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(DriverBasicInfoBo bo) {
        DriverBasicInfo update = MapstructUtils.convert(bo, DriverBasicInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DriverBasicInfo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除驾驶员基本信息信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
